﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Model;

namespace MDMUI.MaterialUI
{
    public partial class HistoryForm : Form
    {
        public HistoryForm()
        {
            InitializeComponent();
            HistoryData.AutoGenerateColumns = false;
            ConfigureDataGridView();
        }

        // 配置DataGridView列
        // 补全后的列配置代码
        private void ConfigureDataGridView()
        {
            // 完整列定义（包含所有历史记录字段）
            var columns = new[]
            {
        new { Name = "HistoryId", Header = "历史ID", Width = 60 },
        new { Name = "MaterialId", Header = "物料ID", Width = 70 },
        new { Name = "ChangeType", Header = "变更类型", Width = 80 },
        new { Name = "ChangeTime", Header = "变更时间", Width = 150 },
        new { Name = "OldMaterialType", Header = "原物料类型", Width = 100 },
        new { Name = "NewMaterialType", Header = "新物料类型", Width = 100 },
        new { Name = "OldDetailedMaterialType", Header = "原详细类型", Width = 100 },
        new { Name = "NewDetailedMaterialType", Header = "新详细类型", Width = 100 },
        new { Name = "OldQuantity", Header = "原数量", Width = 70 },
        new { Name = "NewQuantity", Header = "新数量", Width = 70 },
        new { Name = "OldMaterialUnit", Header = "原单位", Width = 70 },
        new { Name = "NewMaterialUnit", Header = "新单位", Width = 70 },
        new { Name = "OldMaterialDescription", Header = "原描述", Width = 120 },
        new { Name = "NewMaterialDescription", Header = "新描述", Width = 120 },
        new { Name = "OldConsumptionDeduction", Header = "原消耗扣除", Width = 100 },
        new { Name = "NewConsumptionDeduction", Header = "新消耗扣除", Width = 100 },
        new { Name = "OldMaterialGroupNumber", Header = "原组号", Width = 80 },
        new { Name = "NewMaterialGroupNumber", Header = "新组号", Width = 80 }
    };

            // 清空现有列
            HistoryData.Columns.Clear();

            // 添加所有列
            foreach (var col in columns)
            {
                HistoryData.Columns.Add(new DataGridViewTextBoxColumn
                {
                    DataPropertyName = col.Name,
                    HeaderText = col.Header,
                    Width = col.Width,
                    Name = col.Name,
                    AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                });
            }

            // 设置自动调整列宽
            HistoryData.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

        // 加载历史记录数据
        public void LoadHistory(List<Model.Material.History> historyList, string title = "物料历史记录")
        {
            try
            {
                this.Text = title;

                // 绑定数据源
                HistoryData.DataSource = historyList;

                // 自动调整列宽
                HistoryData.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

                // 添加状态标签
                statusLabel.Text = $"共 {historyList?.Count ?? 0} 条历史记录";
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载历史记录失败: {ex.Message}", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 窗体布局调整
        private void HistoryForm_Load(object sender, EventArgs e)
        {
            // 添加状态栏
            statusStrip1.Items.Add(statusLabel);
        }

        // 状态栏标签
        private ToolStripStatusLabel statusLabel = new ToolStripStatusLabel();
    }
}